<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Classifieds extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		// load library
		// load helper
		// load model
		$this->load->model('Classifiedscatg_model','',TRUE);
		$this->load->model('Classified_model','',TRUE);
		
	}
	
	// load index file
	function index($offset=0) {
		$data['title'] = 'Buy/Sell/Trade stuffs online';
		$data['message'] = '';
		$data['action'] = 'details';
		
		//get_result($cond = NULL, $limit = 10, $offset = 0, $sort = NULL
		$cond['is_active'] = 1;
		$limit = $this->config->item('per_page');
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Classified_model->get_result($cond, $limit, $offset, $sort);
		$data['clist'] = $results['rows'];
		$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
		$data['num_rows'] =  $results['num_rows'];
		$data['active'] = 'classifieds';//for menu
		
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('classifieds/index');
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		
		$this->load->view('classifieds/index', $data);
	
	}
	/**
	* Details information of selected ads
	**/
	function details($id)
	{
		$data['title'] = 'Buy/Sell/Trade Stuffs: Details';
		$data['message'] = '';
		$c_id = (int) $id;
		
		$cond['advid'] = $c_id;
		$results =$this->Classified_model->get_result($cond);


		$clist = $results['rows'];
		$c_details = array();
		
		foreach ($clist as $adv)
		{
			$c_details = $adv;
		}
		$otherCond['is_active'] = 1;
		$otherCond['advertiserID'] = $c_details->advertiserID;

		$otherads_result = $this->Classified_model->get_result($otherCond, 4);
		$otherads = $otherads_result['rows'];
		//$auto_details = $autolist['rows'][0];
		$data['backurl'] = 'classifieds/';
		$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
		$data['active'] = 'classifieds';
		$data['cdetails'] = $c_details;
		$data['others'] = $otherads;
		$data['bookmark'] = $c_details->title;
		$this->load->view('classifieds/details', $data);
	}
	//display based on browsing 
	function browse($id, $offset=0)
	{
		$catgID = (int) $id;
		
		$cond['categoryID'] = $catgID;
		$cond['is_active'] = 1;
		//$cond['promote_ad'] = 1;
		
		$limit = $this->config->item('per_page');
		$data['active'] = 'classifieds';
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'asc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Classified_model->get_result($cond, $limit, $offset, $sort);
		$data['clist'] = $results['rows'];
		$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
		$data['num_rows'] = $results['num_rows'];
		
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('classifieds/browse/'. '/'. $catgID );
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		//to display chosen category
		$data['selectedcatg'] = $data['catg'][$catgID];
		$data['bookmark'] = $data['catg'][$catgID];
		$this->load->view('classifieds/index', $data);
	}
	/**
	* advanced search 
	* This function is called when user submit 'search' form
	**/
	function search($offset=0)
	{
		$keyword = NULL;
		if($this->input->post('keyword')){
			$keyword = $this->input->post('keyword');
			}
			$categoryID = 0;

			if ($this->input->post('selectCatg')){
				$cond['categoryID'] = $this->input->post('selectCatg');
				$categoryID = $this->input->post('selectCatg');

			}
			$categoryID = $categoryID;

			$limit = $this->config->item('per_page');
			$cond['is_active'] = 1;
			$sort =array( 
				array(
					'column'=>'posted_date',
					'order' => 'desc'
					),
				array(
					'column' => 'title',
					'order' => 'asc'
					),
			 );

			$results = $this->Classified_model->get_search_result($keyword,$cond, $limit, $offset, $sort);
			//$this->form_data->keyword = $keyword;
			$data['clist'] = $results['rows'];
			$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
			if ($categoryID > 0){
				$data['selectedIndex'] =  $categoryID;
				$data['selectedcatg'] = $data['catg'] [$categoryID];
				
			}
			$data['num_rows'] = $results['num_rows'];
			$data['active'] = 'classifieds';//for menu
			if ($keyword == NULL)
				$keyword = 0;

			//pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url('classifieds/result') . '/'. str_replace(' ', '_', $keyword) . '/'. $categoryID;
			$config['total_rows'] = (int)$data['num_rows'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 5;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['autoBrands'] = get_autos_brand_count();
			$data['propTypes'] = get_property_type_count();
			$data['advTypes'] = get_classfieds_type_count();
			$data['search'] = 'search';
			$data['keyword'] = $keyword; // to identity search
			$data['bookmark'] = 'Search';
			$this->load->view('classifieds/index', $data);
		
	}
	/**
	* advanced search 
	* This function is subsequent calling function for search result (Inclu: Pagination)
	**/
	function result($keyword = NULL, $categoryID = 0, $offset=0)
	{
		
		$cond['is_active'] = 1;
		if ($categoryID != 0){
			$cond['categoryID'] = $categoryID;
		}
		if ($keyword != 0){
			$keyword = str_replace('_', '', $keyword);
		}
		if ($keyword == 0){
			$keyword = NULL;
		}
			$limit = $this->config->item('per_page');
			
			$sort =array( 
				array(
					'column'=>'posted_date',
					'order' => 'desc'
					),
				array(
					'column' => 'title',
					'order' => 'asc'
					),
			 );
			$results = $this->Classified_model->get_search_result($keyword,$cond, $limit, $offset, $sort);
			//$this->form_data->keyword = $keyword;
			$data['clist'] = $results['rows'];
			$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
			if ($categoryID != 0){
				$data['selectedIndex'] =  $categoryID;
				$data['selectedcatg'] = $data['catg'] [$categoryID];
			}
			$data['num_rows'] = $results['num_rows'];
			$data['active'] = 'classifieds';//for menu

			//pagination
			$this->load->library('pagination');
			if ($keyword == NULL){
				$keyword = 0;
			}
			$config = array();
			$config['base_url'] = site_url('classifieds/result') . '/'. str_replace(' ', '_', $keyword) . '/'. $categoryID;
			$config['total_rows'] = (int)$data['num_rows'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 5;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['autoBrands'] = get_autos_brand_count();
			$data['propTypes'] = get_property_type_count();
			$data['advTypes'] = get_classfieds_type_count();
			$data['search'] = 'search'; // to identity search
			$data['keyword'] = $keyword; // to identity search
			$data['bookmark'] = 'Search';
			$this->load->view('classifieds/index', $data);
	}

	function filter($keyword = NULL, $offset=0){
		/**	$("#dropdown_id").change(function(){
	  alert('change the dropdown value');
	})**/
		$cond['type'] =  $keyword;
		$cond['is_active'] = 1;
		//$cond['promote_ad'] = 1;
		
		$limit = $this->config->item('per_page');
		$data['active'] = 'classifieds';
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'asc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Classified_model->get_result($cond, $limit, $offset, $sort);
		$data['clist'] = $results['rows'];
		$data['catg'] = $this->Classifiedscatg_model->get_classifieds_categories();
		$data['num_rows'] = $results['num_rows'];
		
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('classifieds/filter/'. '/'. $keyword );
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		$data['bookmark'] = $this->getCategoryName((int)$keyword);
		$this->load->view('classifieds/index', $data);
	}
	function getCategoryName ($choice = 0)
	{
		switch($choice){
			case 1:
				return 'Sell';break;
			case 2:
				return 'Buy';break;
			case 3:
				return 'Trade';break;
			case 4:
				return 'Rent';break;
			default:
				break;
		}
	}
}